package com.zenithmind.document.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.zenithmind.common.context.UserContext;
import com.zenithmind.common.result.Result;
import com.zenithmind.document.pojo.dto.DocumentCategoryCreateDTO;
import com.zenithmind.document.pojo.dto.DocumentCategoryUpdateDTO;
import com.zenithmind.document.pojo.query.DocumentCategoryQueryDTO;
import com.zenithmind.document.pojo.vo.DocumentCategoryVO;
import com.zenithmind.document.service.DocumentCategoryService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 文档分类管理控制器
 * 
 * @author ZenithMind Team
 * @since 2025-01-09
 */
@Tag(name = "文档分类管理", description = "文档分类管理相关接口")
@RestController
@RequestMapping("/api/v1/document-categories")
@RequiredArgsConstructor
public class DocumentCategoryController {

    private final DocumentCategoryService documentCategoryService;

    @Operation(summary = "创建分类", description = "创建新的文档分类")
    @PostMapping
    public Result<DocumentCategoryVO> createCategory(@Valid @RequestBody DocumentCategoryCreateDTO createDTO) {
        DocumentCategoryVO category = documentCategoryService.createCategory(createDTO);
        return Result.success(category);
    }

    @Operation(summary = "更新分类", description = "更新分类信息")
    @PutMapping("/{id}")
    public Result<Boolean> updateCategory(
            @Parameter(description = "分类ID") @PathVariable String id,
            @Valid @RequestBody DocumentCategoryUpdateDTO updateDTO) {
        Boolean result = documentCategoryService.updateCategory(id, updateDTO);
        return Result.success(result);
    }

    @Operation(summary = "删除分类", description = "删除指定分类")
    @DeleteMapping("/{id}")
    public Result<Boolean> deleteCategory(@Parameter(description = "分类ID") @PathVariable String id) {
        Boolean result = documentCategoryService.deleteCategory(id);
        return Result.success(result);
    }

    @Operation(summary = "批量删除分类", description = "批量删除分类")
    @DeleteMapping("/batch")
    public Result<Boolean> batchDeleteCategories(@RequestBody List<String> ids) {
        Boolean result = documentCategoryService.batchDeleteCategories(ids);
        return Result.success(result);
    }

    @Operation(summary = "分页查询分类", description = "分页查询分类列表")
    @PostMapping("/page")
    public Result<IPage<DocumentCategoryVO>> getCategoryPage(@Valid @RequestBody DocumentCategoryQueryDTO queryDTO) {
        IPage<DocumentCategoryVO> page = documentCategoryService.getCategoryPage(queryDTO);
        return Result.success(page);
    }

    @Operation(summary = "获取分类详情", description = "获取分类详细信息")
    @GetMapping("/{id}")
    public Result<DocumentCategoryVO> getCategoryDetail(@Parameter(description = "分类ID") @PathVariable String id) {
        DocumentCategoryVO category = documentCategoryService.getCategoryDetail(id);
        return Result.success(category);
    }

    @Operation(summary = "获取分类树", description = "获取分类树结构")
    @GetMapping("/tree")
    public Result<List<DocumentCategoryVO>> getCategoryTree(
            @Parameter(description = "父分类ID") @RequestParam(required = false) String parentId,
            @Parameter(description = "是否包含禁用的分类") @RequestParam(defaultValue = "false") Boolean includeDisabled) {
        List<DocumentCategoryVO> tree = documentCategoryService.getCategoryTree(parentId, includeDisabled);
        return Result.success(tree);
    }

    @Operation(summary = "获取子分类", description = "获取指定分类的子分类")
    @GetMapping("/{parentId}/children")
    public Result<List<DocumentCategoryVO>> getChildCategories(
            @Parameter(description = "父分类ID") @PathVariable String parentId,
            @Parameter(description = "是否包含禁用的分类") @RequestParam(defaultValue = "false") Boolean includeDisabled) {
        List<DocumentCategoryVO> children = documentCategoryService.getChildCategories(parentId, includeDisabled);
        return Result.success(children);
    }

    @Operation(summary = "获取分类路径", description = "获取分类的完整路径")
    @GetMapping("/{id}/path")
    public Result<List<DocumentCategoryVO>> getCategoryPath(@Parameter(description = "分类ID") @PathVariable String id) {
        List<DocumentCategoryVO> path = documentCategoryService.getCategoryPath(id);
        return Result.success(path);
    }

    @Operation(summary = "搜索分类", description = "根据关键词搜索分类")
    @GetMapping("/search")
    public Result<List<DocumentCategoryVO>> searchCategories(
            @Parameter(description = "关键词") @RequestParam String keyword,
            @Parameter(description = "限制数量") @RequestParam(defaultValue = "20") Integer limit) {
        List<DocumentCategoryVO> categories = documentCategoryService.searchCategories(keyword, limit);
        return Result.success(categories);
    }

    @Operation(summary = "获取热门分类", description = "获取热门分类列表")
    @GetMapping("/hot")
    public Result<List<DocumentCategoryVO>> getHotCategories(
            @Parameter(description = "限制数量") @RequestParam(defaultValue = "10") Integer limit) {
        List<DocumentCategoryVO> categories = documentCategoryService.getHotCategories(limit);
        return Result.success(categories);
    }

    @Operation(summary = "获取我的分类", description = "获取用户创建的分类")
    @GetMapping("/my")
    public Result<List<DocumentCategoryVO>> getUserCategories(
            @Parameter(description = "限制数量") @RequestParam(defaultValue = "20") Integer limit) {
        String userId = UserContext.getUserId();
        List<DocumentCategoryVO> categories = documentCategoryService.getUserCategories(userId, limit);
        return Result.success(categories);
    }

    @Operation(summary = "移动分类", description = "移动分类到新的父分类下")
    @PostMapping("/{id}/move")
    public Result<Boolean> moveCategory(
            @Parameter(description = "分类ID") @PathVariable String id,
            @Parameter(description = "新父分类ID") @RequestParam(required = false) String newParentId) {
        Boolean result = documentCategoryService.moveCategory(id, newParentId);
        return Result.success(result);
    }

    @Operation(summary = "调整分类排序", description = "调整分类排序")
    @PostMapping("/reorder")
    public Result<Boolean> reorderCategories(
            @Parameter(description = "分类ID列表") @RequestBody List<String> categoryIds,
            @Parameter(description = "父分类ID") @RequestParam(required = false) String parentId) {
        Boolean result = documentCategoryService.reorderCategories(categoryIds, parentId);
        return Result.success(result);
    }

    @Operation(summary = "复制分类", description = "复制现有分类")
    @PostMapping("/{id}/copy")
    public Result<DocumentCategoryVO> copyCategory(
            @Parameter(description = "分类ID") @PathVariable String id,
            @Parameter(description = "新分类名称") @RequestParam String newName,
            @Parameter(description = "父分类ID") @RequestParam(required = false) String parentId) {
        DocumentCategoryVO category = documentCategoryService.copyCategory(id, newName, parentId);
        return Result.success(category);
    }

    @Operation(summary = "更新分类文档数量", description = "更新分类的文档数量统计")
    @PostMapping("/{id}/update-count")
    public Result<Boolean> updateCategoryDocumentCount(@Parameter(description = "分类ID") @PathVariable String id) {
        Boolean result = documentCategoryService.updateCategoryDocumentCount(id);
        return Result.success(result);
    }

    @Operation(summary = "批量更新文档数量", description = "批量更新分类的文档数量统计")
    @PostMapping("/batch/update-count")
    public Result<Boolean> batchUpdateCategoryDocumentCount(@RequestBody List<String> categoryIds) {
        Boolean result = documentCategoryService.batchUpdateCategoryDocumentCount(categoryIds);
        return Result.success(result);
    }

    @Operation(summary = "获取分类统计", description = "获取分类统计信息")
    @GetMapping("/{id}/statistics")
    public Result<DocumentCategoryVO> getCategoryStatistics(@Parameter(description = "分类ID") @PathVariable String id) {
        DocumentCategoryVO statistics = documentCategoryService.getCategoryStatistics(id);
        return Result.success(statistics);
    }

    @Operation(summary = "检查分类名称", description = "检查分类名称是否存在")
    @GetMapping("/check-name")
    public Result<Boolean> checkCategoryNameExists(
            @Parameter(description = "分类名称") @RequestParam String name,
            @Parameter(description = "父分类ID") @RequestParam(required = false) String parentId,
            @Parameter(description = "排除的分类ID") @RequestParam(required = false) String excludeId) {
        Boolean exists = documentCategoryService.checkCategoryNameExists(name, parentId, excludeId);
        return Result.success(exists);
    }

    @Operation(summary = "检查是否可删除", description = "检查分类是否可以删除")
    @GetMapping("/{id}/can-delete")
    public Result<Boolean> canDeleteCategory(@Parameter(description = "分类ID") @PathVariable String id) {
        Boolean canDelete = documentCategoryService.canDeleteCategory(id);
        return Result.success(canDelete);
    }
}
